//
//  SettingFirstCell.m
//  Weibbo
//
//  Created by 李 林刚 on 13-8-11.
//  Copyright (c) 2013年 Omnivores. All rights reserved.
//

#import "SettingFirstCell.h"

@interface SettingFirstCell ()<ShareMangerDelegate>
@property (nonatomic, retain)UIImageView  *iconView;
@property (nonatomic, retain)UILabel      *shareNameLab;
@property (nonatomic, retain)UILabel      *bindLab;
@property (nonatomic, retain)UIButton      *bindBtn;

@end

@implementation SettingFirstCell

@synthesize iconView;
@synthesize shareNameLab;
@synthesize bindLab;
@synthesize bindBtn;

-(void)dealloc
{
    UIRelease(iconView);
    UIRelease(shareNameLab);
    UIRelease(bindBtn);
    UIRelease(bindLab);
    [super dealloc];
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        iconView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 24, 24)];
        [self.contentView addSubview:iconView];
        shareNameLab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(iconView.frame)+10, CGRectGetMinY(iconView.frame), 100, CGRectGetHeight(iconView.frame))];
        shareNameLab.font = UIFontOfSize(15);
        shareNameLab.backgroundColor = UIClearColor;
        [shareNameLab setTextAlignment:NSTextAlignmentLeft];
        [shareNameLab setTextColor:UIBlackColor];
        [self.contentView addSubview:shareNameLab];
        
        bindLab = [[UILabel alloc]initWithFrame:CGRectMake(180, CGRectGetMinY(iconView.frame), 55, CGRectGetHeight(iconView.frame))];
        bindLab.backgroundColor = UIClearColor;
        [bindLab setTextColor:UIDarkGrayColor];
        [bindLab setTextAlignment:NSTextAlignmentLeft];
        [self.contentView addSubview:bindLab];
        
        bindBtn = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(bindLab.frame),10,55,29)];
        [bindBtn addTarget:self action:@selector(BindClickEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:bindBtn];
        
    }
    return self;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)UpdateBindCell
{
       BOOL   isBind = [[ShareManger SharedInstance] IsBind:self.tag];
        [iconView setImage:[UIImage imageNamed:@"sina"]];
        [shareNameLab setText:self.tag==ShareTypeSina?@"新浪微博":@"QQ空间"];
        [bindLab setText:isBind?@"已绑定":@"未绑定"];
        [bindBtn setBackgroundImage:[UIImage imageNamed:isBind?@"bind_normal":@""] forState:UIControlStateNormal];
        [bindBtn setBackgroundImage:[UIImage imageNamed:isBind?@"bind_select":@""] forState:UIControlStateHighlighted];
}

-(void)BindClickEvent:(id)sender
{
    if ([[ShareManger SharedInstance]IsBind:self.tag]) {
        [[ShareManger SharedInstance]setDelegate:self];
        [[ShareManger SharedInstance]LoginOut:self.tag];
    }
    else
    {
        [[ShareManger SharedInstance]setDelegate:self];
        [[ShareManger SharedInstance]LogInWithLogInType:self.tag];
    }
}

#pragma mark    shareManageDelegate
-(void)ShareMangerShareType:(ShareType)type DidLoginIn:(BOOL)IsLogin
{
    [[ShareManger SharedInstance]setDelegate:nil];
    if (IsLogin) {
        [self UpdateBindCell];
        [AlertView ShowAlterViewTitle:nil Message:type==ShareTypeSina?@"新浪微博绑定成功":@"QQ空间绑定成功"];
    }
    else
    {
        [AlertView ShowAlterViewTitle:nil Message:type==ShareTypeSina?@"新浪微博绑定失败":@"QQ空间绑定失败"];
    }
}

-(void)ShareMangerShareType:(ShareType)type DidLoginOut:(BOOL)IsOut
{
    [[ShareManger SharedInstance]setDelegate:nil];
    if (IsOut) {
        [self UpdateBindCell];
        [AlertView ShowAlterViewTitle:nil Message:type==ShareTypeSina?@"解除新浪微博绑定成功":@"解除QQ空间绑定成功"];
    }
    else
    {
        [AlertView ShowAlterViewTitle:nil Message:type==ShareTypeSina?@"解除新浪微博绑定失败":@"解除QQ空间绑定失败"];
    }
}
@end
